var jycode;
var ipaddr;
$(document).ready(function () {
    //页面时间
    showTime();
    jycode=getValue("jycode");
    getipaddress();

});

//页面显示时间
function showTime() {
    let today = new Date();
    let y = today.getFullYear();
    let m = today.getMonth() + 1;
    let d = today.getDate();
    let h = today.getHours();
    let mm = today.getMinutes();
    let week = today.getDay();
    let w = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    mm = checkMinutes(mm);
    $('.hright_item1').text(y + '年' + m + '月' + d + '日');
    $('.hright_item2').text(w[week]);
    $('.hright_item3').text(h + ':' + mm);
    setTimeout('showTime()', 500)
}

//验证时间内的minutes
function checkMinutes(mm) {
    if (mm < 10) {
        mm = '0' + mm;
    }
    return mm;
}

function rqList(num,nowDate) {
    for(let i=0;i<num;i++){
        main.rqList.push(getMyDate(new Date(nowDate.getTime()-24*60*60*1000*(i+1))))
        console.log(main.rqList)
    }
}
// 日期
function getMyDate(str) {
    var oDate= new Date(str),
        oYear=oDate.getFullYear(),
        oMonth=oDate.getMonth()+1,
        oDay=oDate.getDate(),
        oHour=oDate.getHours(),
        oMinute=oDate.getMinutes(),
        oSecond=oDate.getSeconds(),
        oTime=oYear+addZero(oMonth)+addZero(oDay)+addZero(oHour)+addZero(oMinute)+addZero(oSecond);
    return oTime;
}

// 日期 yyyy-mm-dd hh:mm:ss
function getMyDate2(str) {
    var oDate= new Date(str),
        oYear=oDate.getFullYear(),
        oMonth=oDate.getMonth()+1,
        oDay=oDate.getDate(),
        oHour=oDate.getHours(),
        oMinute=oDate.getMinutes(),
        oSecond=oDate.getSeconds(),
        oTime=oYear+'-'+addZero(oMonth)+'-'+addZero(oDay)+' '+addZero(oHour)+':'+addZero(oMinute)+':'+addZero(oSecond);
    return oTime;
}
//日期差
function getDay(date1,date2) {
    var s= new Date(date1)-new Date(date2);
    var iday=parseInt(Math.abs(s)/1000/60/60/24);
    return iday+1;
}
function addZero(num) {
    if(parseInt(num)<10){
        num="0"+num;
    }else {
        num=num.toString();
    }
    return num;
}

function return_home(){

    // window.open('http://41.230.75.4:8080/nxpbrain/page/index.html?jycode='+ getValue('jycode'));
    //location.replace('http://41.230.75.4:8080/nxpbrain/login/desktop?policeNumber='+ getValue('jycode'))
}

//获得URL的参数
function getValue(str) {
    let urlInfo = window.location.href;
    let urlArr=urlInfo.split(/[?,#]/);
    if(urlArr.length>1&&urlArr[1].indexOf(str)>-1){
        var query=urlArr[1];
        var obj={};
        var arr=query.split("&");
        for(var i=0;i<arr.length;i++){
            arr[i]=arr[i].split("=");
            obj[arr[i][0]]=arr[i][1];
        }
        return obj[str];
    }else {
        return null;
    }
}


//获取ip
function getipaddress() {
    var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

    if (RTCPeerConnection) (function () {
        debugger;
        var rtc = new RTCPeerConnection({iceServers:[]});
        if (1 || window.mozRTCPeerConnection) {
            rtc.createDataChannel('', {reliable:false});
        };

        rtc.onicecandidate = function (evt) {
            if (evt.candidate) grepSDP("a="+evt.candidate.candidate);
        };
        rtc.createOffer(function (offerDesc) {
            grepSDP(offerDesc.sdp);
            rtc.setLocalDescription(offerDesc);
        }, function (e) { console.warn("offer failed", e); });


        var addrs = Object.create(null);
        addrs["0.0.0.0"] = false;
        function updateDisplay(newAddr) {
            if (newAddr in addrs) return;
            else addrs[newAddr] = true;
            var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
            for(var i = 0; i < displayAddrs.length; i++){
                if(displayAddrs[i].length > 16){
                    displayAddrs.splice(i, 1);
                    i--;
                }
            }
            console.log(displayAddrs[0]);
            ipaddr= displayAddrs[0];//打印出本地电脑ip
        }

        function grepSDP(sdp) {
            var hosts = [];
            sdp.split('\r\n').forEach(function (line, index, arr) {
                if (~line.indexOf("a=candidate")) {
                    var parts = line.split(' '),
                        addr = parts[4],
                        type = parts[7];
                    if (type === 'host') updateDisplay(addr);
                } else if (~line.indexOf("c=")) {
                    var parts = line.split(' '),
                        addr = parts[2];
                    updateDisplay(addr);
                }
            });
        }
    })();
    else{
        console.log("请使用主流浏览器");
    }
}
